/*
	Copyright (C) 2010 2011 Rémi Thebault

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef GANTTSCENE_H
#define GANTTSCENE_H

#include <QGraphicsLineItem>
#include <QGraphicsRectItem>
#include <QGraphicsScene>

#include <procycle/cycle.h>

#include "globals.h"
#include "pcchild.h"


class GanttGrads;
class GanttTask;
class GanttMTask;
class GanttCycle;
class GanttLink;
class TimeAxis;


static const QString DEFAULT_ATASK_COLOR = "#7f7fff";
static const QString DEFAULT_MTASK_COLOR = "#5a5a60";

class GanttScene : public QGraphicsScene, public PCChild
{
	Q_OBJECT
public:


	GanttScene(ProCycle* app, QRectF rect, QObject *parent = 0);
	virtual ~GanttScene() {
	}

    void initSizePolicy();

	static GanttTask* createGTask(ProCycle* app, PC::Task* task, GanttCycle* gcycle, GanttMTask* parent=0);

	PC::Cycle* cycle() {
		return m_cycle;
	}

	void resetCycle(PC::Cycle* cycle);

	GanttCycle*	ganttCycle() {
		return m_ganttCycle;
	}

    TimeAxis* axis() const;

	GanttTask* resolvTask(QString tbsId);

	GanttTask* taskAt(const QPointF& p);


	void editModeChanged();

    QSize preferredSize() const {
        return m_preferredSize;
    }


	static QString colorStrForTask(PC::Task* task);
	static QColor colorForTask(PC::Task* task);
	static QColor borderColor(const QColor& back);

public slots:

	void processExpansionChange(PC::Task* task);

	void processTaskTimeChanged(PC::Task*);
	void processTaskValueChanged(PC::Task* task, PC::Task::Data data);

	void processTaskCreated(PC::MacroTask* parent, PC::Task* task);
	void processTaskWillDelete(PC::Task* task);
	void processTaskTransformed(PC::MacroTask* parent, int taskPos);
	void processTaskMoved(PC::MacroTask* parentTo, PC::Task* parentFrom, int posTo, int posFrom);

	void processLinkCreated(PC::Link* link);
	void processLinkWillDelete(PC::Link* link);

	void processScheduled();

	void selectTask(PC::Task* selected, PC::Task* unselected);
	void selectLink(PC::Link* selected, PC::Link* unselected);

	void toggleCriticalPath(bool on);
	void toggleCycleOverlap(bool on);
	void toggleTasksFloat(bool on);
    void toggleTaskName(bool on);


    void zoomFit();
    void zoomIn();
    void zoomOut();
    void toggleZoomWindow(bool on);


protected:

    void wheelEvent(QGraphicsSceneWheelEvent *event);

    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

    void keyReleaseEvent(QKeyEvent *event);

private:

    void widthResize();
    void heightResize();

	PC::Cycle*			m_cycle;

    GanttGrads*			m_grads;
	GanttCycle*			m_ganttCycle;

	QGraphicsLineItem*	m_cycleTime;

    bool                m_windowZoomMode;
    int                 m_windowZoomStart;
    bool                m_windowZoomCancelled;
    QGraphicsLineItem*	m_windowZoomLine1;
    QGraphicsLineItem*	m_windowZoomLine2;
    QGraphicsRectItem*	m_windowZoomRect1;
    QGraphicsRectItem*	m_windowZoomRect2;

    QSize m_preferredSize;

};

#endif // GANTTSCENE_H
